<?php

namespace app\models\system;

use Yii;

/**
 * This is the model class for table "set_home".
 *
 * @property int $id
 * @property int $day 每日上新显示数量
 * @property int $group 推荐拼团显示数量
 * @property int $brand 热门品牌显示数量
 * @property int $product 畅销好物显示数量
 * @property int $brand_total 品牌商品数量
 * @property int $nav_total 导航一行排几个
 */
class SetHome extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'set_home';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['day', 'group', 'brand', 'product'], 'integer'],

            [['brand_total', 'nav_total'], 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'day' => 'Day',
            'group' => 'Group',
            'brand' => 'Brand',
            'product' => 'Product',
        ];
    }

    public static function getLastOne()
    {
        $model = self::find()
            ->orderBy(['id' => SORT_DESC])
            ->one();

        if (!$model) {
            $model = new self();
        }

        return $model;
    }

    public static function saveData($data)
    {
        $model = self::getLastOne();

        if ($model->load(['SetHome' => $data]) && $model->save()) {
            return true;
        }
    }
}
